package cn.buk.tms.flight.dto.response;

import cn.buk.common.flight.dto.FlightBasicInfo;
import cn.buk.tms.entity.order.flight.FlightChangeOrder;
import cn.buk.tms.entity.order.flight.FlightChangeOrderDetail;
import cn.buk.tms.common.dto.CustomerDto;
import cn.buk.tms.common.dto.PaymentMethodDto;
import cn.buk.tms.common.dto.SupplierDto;
import cn.buk.tms.common.dto.flight.BaseChangeOrderDto;
import cn.buk.tms.common.dto.flight.BaseChangeOrderFlightDto;
import cn.buk.tms.dto.AttachmentDto;
import cn.buk.tms.dto.SmsDetailDto;
import cn.buk.common.util.DateUtil;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yfdai
 */
public class ChangeOrderResponseDto extends BaseChangeOrderDto {

  /**
   * 外部订单号（cz b2b)
   */
  private String externalOrderNo;

  /**
   * 原来的大编码，用于航司的B2B
   */
  private String oldBigPnr;

  /**
   * 新的大编码，用于航司的B2B
   */
  private String newBigPnr;

  private List<AttachmentDto> attachments;

  public static ChangeOrderResponseDto createdBy(FlightChangeOrder o) {
    ChangeOrderResponseDto dto = new ChangeOrderResponseDto();

    BeanUtils.copyProperties(o, dto, "flights", "customer");

    if (o.getCustomer() != null) {
      dto.setCustomer(CustomerDto.create(o.getCustomer().getId(), o.getCustomer().getVipName(), o.getCustomer().getCustomerCode()));
    } else {
      dto.setCustomer(CustomerDto.create(0, null, null));
    }

    dto.setParValue(o.getParvalue());
    dto.setReasonCode(o.getChangeType());

    dto.setOrderStatus(o.getStatus());

    for(FlightChangeOrderDetail detail: o.getFlights()) {
      BaseChangeOrderFlightDto flightDto = new BaseChangeOrderFlightDto();
      BeanUtils.copyProperties(detail.getFlight(), flightDto);

      try {
        flightDto.setDdate(DateUtil.convertToDate(detail.getFlight().getDdate(), "yyyy-MM-dd"));
      } catch (Exception ignored) {
      }
      flightDto.setFltType(detail.getFlightType());
      flightDto.setId(detail.getId());

      dto.getFlights().add(flightDto);
    }

    var flights = dto.getFlights().stream()
            .sorted(Comparator.comparing(BaseChangeOrderFlightDto::getFltType))
            //todo: getDdate()会出现null, 如何修正？
//            .sorted(Comparator.comparing(BaseChangeOrderFlightDto::getDdate))
            .collect(Collectors.toList());
    dto.setFlights(flights);

    if (o.getSupplier() != null) {
      dto.setSupplier(SupplierDto.create(o.getSupplier().getId(), o.getSupplier().getName()));
    }

    if (o.getPaymentMethod() != null) {
      dto.setPaymentMethod(PaymentMethodDto.create(o.getPaymentMethod().getId(), o.getPaymentMethod().getName()));
    }


    return dto;
  }

  /**
   * 乘机人的联系电话
   */
  private String mobile;

  /**
   * 出票人、处理人
   */
  private String ticketer;

  /**
   * 原编码(如果有）
   */
  private String oldPnrNo;

  /**
   * 新编码(如果有）
   */
  private String newPnrNo;

  /**
   * 改签类型
   * 0-升舱
   * 1-更改
   * 2-签转
   */
  private int changeType;


  private SupplierDto supplier;

  private PaymentMethodDto paymentMethod;

  /**
   * DETR刷新状态
   * 1 - 已刷新
   * 2 - 已更新到FlightOrderTicket中
   * 4 - 找不到，应该是获取不到DETR信息，非授权OFFICE开票
   * 8 - 票号状态已到终态，无需再检查
   */
  private Integer detrRefreshStatus;

  /**
   * 根据票号获取DETR信息中包含的航班信息
   */
  private List<FlightBasicInfo> detrFlights;

  private List<SmsDetailDto> smsDetails;

  public int getStatus() {
    return this.getOrderStatus();
  }

  public SupplierDto getSupplier() {
    return supplier;
  }

  public void setSupplier(SupplierDto supplier) {
    this.supplier = supplier;
  }

  public PaymentMethodDto getPaymentMethod() {
    return paymentMethod;
  }

  public void setPaymentMethod(PaymentMethodDto paymentMethod) {
    this.paymentMethod = paymentMethod;
  }

  public String getOldPnrNo() {
    return oldPnrNo;
  }

  public void setOldPnrNo(String oldPnrNo) {
    this.oldPnrNo = oldPnrNo;
  }

  public String getNewPnrNo() {
    return newPnrNo;
  }

  public void setNewPnrNo(String newPnrNo) {
    this.newPnrNo = newPnrNo;
  }

  public String getTicketer() {
    return ticketer;
  }

  public void setTicketer(String ticketer) {
    this.ticketer = ticketer;
  }

  public String getMobile() {
    return mobile;
  }

  public void setMobile(String mobile) {
    this.mobile = mobile;
  }

  public int getDetrRefreshStatus() {
    return detrRefreshStatus == null ? 0 : detrRefreshStatus;
  }

  public void setDetrRefreshStatus(Integer detrRefreshStatus) {
    this.detrRefreshStatus = detrRefreshStatus;
  }

  public int getChangeType() {
    return changeType;
  }

  public void setChangeType(int changeType) {
    this.changeType = changeType;
  }

  public List<FlightBasicInfo> getDetrFlights() {
    if (detrFlights == null) {
      detrFlights = new ArrayList<>();
    }
    return detrFlights;
  }

  public void setDetrFlights(List<FlightBasicInfo> detrFlights) {
    this.detrFlights = detrFlights;
  }

  public List<SmsDetailDto> getSmsDetails() {
    if (smsDetails == null) {
      smsDetails = new ArrayList<>();
    }
    return smsDetails;
  }

  public void setSmsDetails(List<SmsDetailDto> smsDetails) {
    this.smsDetails = smsDetails;
  }

  public List<AttachmentDto> getAttachments() {
    if (attachments == null) {
      attachments = new ArrayList<>();
    }
    return attachments;
  }

  public void setAttachments(List<AttachmentDto> attachments) {
    this.attachments = attachments;
  }

  public String getExternalOrderNo() {
    return externalOrderNo;
  }

  public void setExternalOrderNo(String externalOrderNo) {
    this.externalOrderNo = externalOrderNo;
  }

  public String getOldBigPnr() {
    return oldBigPnr;
  }

  public void setOldBigPnr(String oldBigPnr) {
    this.oldBigPnr = oldBigPnr;
  }

  public String getNewBigPnr() {
    return newBigPnr;
  }

  public void setNewBigPnr(String newBigPnr) {
    this.newBigPnr = newBigPnr;
  }
}
